#include "confighandler.h"
#include "common.h"
#include <QDir>
#include <QSettings>
#include <QApplication>
#include <QStandardPaths>
#include <QDebug>
#include <iostream>
#include <typeinfo>
#include <QQmlEngine>
using namespace std;

void GlobalConfig::initConfig()
{
    QString cfgPath = QStandardPaths::writableLocation(QStandardPaths::DataLocation) + "/global_config.ini";
    QFile configFile(cfgPath);
    if(!configFile.exists()) {
        QFile::copy(QCoreApplication::applicationDirPath() + "/global_config.ini", \
        cfgPath);
    }

    mSetting = new QSettings(cfgPath, QSettings::IniFormat, this);
    // cloud
    mSetting->beginGroup(CLOUD_GROUP);
    m_fileMap[CLOUD_GROUP][KEY_USERNAME] = mSetting->value(KEY_USERNAME, "").toString();
    m_fileMap[CLOUD_GROUP][KEY_USERPWD] = mSetting->value(KEY_USERPWD, "").toString();
    m_fileMap[CLOUD_GROUP][KEY_ACCOUNT_TYPE] = mSetting->value(KEY_ACCOUNT_TYPE, 0).toInt();
    m_fileMap[CLOUD_GROUP][KEY_SERVERTAG] = mSetting->value(KEY_SERVERTAG, 0).toInt();
    m_fileMap[CLOUD_GROUP][KEY_CONUTRY] = mSetting->value(KEY_CONUTRY, "US").toString();
    m_fileMap[CLOUD_GROUP][KEY_LOGIN_STATE] = mSetting->value(KEY_LOGIN_STATE, false).toBool();
    m_fileMap[CLOUD_GROUP][KEY_REMEMBER_ME] = mSetting->value(KEY_REMEMBER_ME, false).toBool();
    mSetting->endGroup();

    // launch
    mSetting->beginGroup(LAUNCH_GROUP);
    m_fileMap[LAUNCH_GROUP][KEY_LAUNCH_COUNT] = mSetting->value(KEY_LAUNCH_COUNT, 0).toInt();
    mSetting->endGroup();

    // layout
    mSetting->beginGroup(LAYOUT_GROUP);
    m_fileMap[LAYOUT_GROUP][KEY_PAM_CHECK] =  mSetting->value(KEY_PAM_CHECK, true).toBool();
    m_fileMap[LAYOUT_GROUP][KEY_PSM_CHECK] =  mSetting->value(KEY_PSM_CHECK, false).toBool();
    m_fileMap[LAYOUT_GROUP][KEY_MODEL_SPACING] = mSetting->value(KEY_MODEL_SPACING, 2.0).toDouble();
    m_fileMap[LAYOUT_GROUP][KEY_PLATFORM_MARGINS] =  mSetting->value(KEY_PLATFORM_MARGINS, 2.0).toDouble();
    m_fileMap[LAYOUT_GROUP][KEY_RSAZA_INDEX] =  mSetting->value(KEY_RSAZA_INDEX, 1).toInt();
    m_fileMap[LAYOUT_GROUP][KEY_SBA_CHECK] =  mSetting->value(KEY_SBA_CHECK, true).toBool();
    m_fileMap[LAYOUT_GROUP][KEY_SBH_CHECK] =  mSetting->value(KEY_SBH_CHECK, false).toBool();
    m_fileMap[LAYOUT_GROUP][KEY_START_POSITION_INDEX] =  mSetting->value(KEY_START_POSITION_INDEX, 0).toInt();
    m_fileMap[LAYOUT_GROUP][KEY_MTCAN_CHECK] =  mSetting->value(KEY_MTCAN_CHECK, false).toBool();
    mSetting->endGroup();

    //qin -- punch
    mSetting->beginGroup(PUNCH_GROUP);
    m_fileMap[PUNCH_GROUP][KEY_PUNCH_OCD_SPINBOXTEXT] = mSetting->value(KEY_PUNCH_OCD_SPINBOXTEXT, 2.0).toDouble();
    m_fileMap[PUNCH_GROUP][KEY_PUNCH_ICD_SPINBOXTEXT] = mSetting->value(KEY_PUNCH_ICD_SPINBOXTEXT, 2.0).toDouble();
    m_fileMap[PUNCH_GROUP][KEY_PUNCH_EL_SPINBOXTEXT] = mSetting->value(KEY_PUNCH_EL_SPINBOXTEXT, 2.0).toDouble();
    m_fileMap[PUNCH_GROUP][KEY_PUNCH_GD_SPINBOXTEXT] = mSetting->value(KEY_PUNCH_GD_SPINBOXTEXT, 2.0).toDouble();
    m_fileMap[PUNCH_GROUP][KEY_PUNCH_DIRECTION_INDEX] = mSetting->value(KEY_PUNCH_DIRECTION_INDEX, 0).toInt();
    m_fileMap[PUNCH_GROUP][KEY_PUNCH_KEEPHOLE_CHECK] = mSetting->value(KEY_PUNCH_KEEPHOLE_CHECK, false).toBool();
    mSetting->endGroup();

    //qin -- hollow
    mSetting->beginGroup(HOLLOW_GROUP);
    m_fileMap[HOLLOW_GROUP][KEY_HOLLOW_WT_SPINBOXTEXT] = mSetting->value(KEY_HOLLOW_WT_SPINBOXTEXT, 2.0).toDouble();
    m_fileMap[HOLLOW_GROUP][KEY_HOLLOW_ACCURACY_SPINBOXTEXT] = mSetting->value(KEY_HOLLOW_ACCURACY_SPINBOXTEXT, 1.0).toDouble();
    m_fileMap[HOLLOW_GROUP][KEY_HOLLOW_DIRECTION_INDEX] = mSetting->value(KEY_HOLLOW_DIRECTION_INDEX, 0).toInt();
    m_fileMap[HOLLOW_GROUP][KEY_HOLLOW_NBF_INDEX] = mSetting->value(KEY_HOLLOW_NBF_INDEX, 0).toInt();
    m_fileMap[HOLLOW_GROUP][KEY_HOLLOW_CELLSIZE_SPINBOXTEXT] = mSetting->value(KEY_HOLLOW_CELLSIZE_SPINBOXTEXT, 5.0).toDouble();
    m_fileMap[HOLLOW_GROUP][KEY_HOLLOW_SRADIUS_SPINBOXTEXT] = mSetting->value(KEY_HOLLOW_SRADIUS_SPINBOXTEXT, 1.0).toDouble();
    mSetting->endGroup();

    //qin -- recent open file
    mSetting->beginGroup(RECENTOPENFILE_GROUP);
    m_fileMap[RECENTOPENFILE_GROUP][KEY_RECENTOPENFILE_TEXT] = mSetting->value(KEY_RECENTOPENFILE_TEXT, "").toString();
    mSetting->endGroup();

    // repair
    mSetting->beginGroup(REPAIR_GROUP);
    m_fileMap[REPAIR_GROUP][KEY_MODEL_QUALITY_CHECK] =  mSetting->value(KEY_MODEL_QUALITY_CHECK, true).toBool();
    m_fileMap[REPAIR_GROUP][KEY_SMRPD_CHECK] =  mSetting->value(KEY_SMRPD_CHECK, true).toBool();
    mSetting->endGroup();

    // support
    mSetting->beginGroup(SUPPORT_GROUP);
    m_fileMap[SUPPORT_GROUP][KEY_SUPPORT_CHECKBOX] =  mSetting->value(KEY_SUPPORT_CHECKBOX, true).toBool();
    mSetting->endGroup();

    // text
    mSetting->beginGroup(TEXT_GROUP);
    m_fileMap[TEXT_GROUP][KEY_TEXT_ANGLE] =  mSetting->value(KEY_TEXT_ANGLE, 0).toInt();
    m_fileMap[TEXT_GROUP][KEY_TEXT_HEIGHT] =  mSetting->value(KEY_TEXT_HEIGHT, 5.0).toDouble();
    m_fileMap[TEXT_GROUP][KEY_TEXT_TEXT] =  mSetting->value(KEY_TEXT_TEXT, "Anycubic").toString();
    m_fileMap[TEXT_GROUP][KEY_FAMILY_INDEX] =  mSetting->value(KEY_FAMILY_INDEX, 0).toInt();
    m_fileMap[TEXT_GROUP][KEY_FONT_POINT_INDEX] =  mSetting->value(KEY_FONT_POINT_INDEX, 0).toInt();
    mSetting->endGroup();

    // base
    mSetting->beginGroup(BASE_CONFIG_GROUP);
    m_fileMap[BASE_CONFIG_GROUP][KEY_CUSTOM_SUFFIX] = mSetting->value(KEY_CUSTOM_SUFFIX, "").toString();
    m_fileMap[BASE_CONFIG_GROUP][KEY_INTELLIGENT_OPEN] = mSetting->value(KEY_INTELLIGENT_OPEN, false).toBool();
    m_fileMap[BASE_CONFIG_GROUP][KEY_FIRST_FAST_RESIN] = mSetting->value(KEY_FIRST_FAST_RESIN, false).toBool();
    m_fileMap[BASE_CONFIG_GROUP][KEY_FIRST_HIGH_RESIN] = mSetting->value(KEY_FIRST_HIGH_RESIN, false).toBool();
    m_fileMap[BASE_CONFIG_GROUP][KEY_TEST_ENVIRONMENT] = mSetting->value(KEY_TEST_ENVIRONMENT, false).toBool();
    m_fileMap[BASE_CONFIG_GROUP][KEY_ENVIRONMENT] = mSetting->value(KEY_ENVIRONMENT, "").toString();
    m_fileMap[BASE_CONFIG_GROUP][KEY_UNDOVIEW] = mSetting->value(KEY_UNDOVIEW, false).toBool();

    mSetting->endGroup();

    // path
    mSetting->beginGroup(FILE_PATH_CONFIG_GROUP);
    m_fileMap[FILE_PATH_CONFIG_GROUP][FACEGEN_FILE_PATH] = mSetting->value(FACEGEN_FILE_PATH, "").toString();
    m_fileMap[FILE_PATH_CONFIG_GROUP][PWSP_FILE_PATH] = mSetting->value(PWSP_FILE_PATH, "").toString();
    m_fileMap[FILE_PATH_CONFIG_GROUP][PWSR_FILE_PATH] = mSetting->value(PWSR_FILE_PATH, "").toString();
    m_fileMap[FILE_PATH_CONFIG_GROUP][MODEL_FILE_PATH] = mSetting->value(MODEL_FILE_PATH, "").toString();
    mSetting->endGroup();

    updateNewFeatures();
}

void GlobalConfig::updateNewFeatures() {
    QString cfgPath = QCoreApplication::applicationDirPath() + "/global_config.ini";
    QFile configFile(cfgPath);
    QStringList listSuffix;
    listSuffix.clear();
    if(configFile.exists()) {
        QSettings setting(cfgPath, QSettings::IniFormat);
        // 同步新版本后缀
        setting.beginGroup(BASE_CONFIG_GROUP);
        QString suffix = setting.value(KEY_CUSTOM_SUFFIX).toString();
        listSuffix += m_fileMap[BASE_CONFIG_GROUP][KEY_CUSTOM_SUFFIX].toString().split(",");
        listSuffix += suffix.split(",");
        listSuffix.removeDuplicates();
        suffix = listSuffix.join(",");
        m_fileMap[BASE_CONFIG_GROUP][KEY_CUSTOM_SUFFIX] = suffix;
        m_fileMap[BASE_CONFIG_GROUP][KEY_TEST_ENVIRONMENT] = setting.value(KEY_TEST_ENVIRONMENT, false).toBool();

        // 3.2.0同步云API url
        QString envir = setting.value(KEY_ENVIRONMENT).toString();
        m_fileMap[BASE_CONFIG_GROUP][KEY_ENVIRONMENT] = envir;
        setting.endGroup();

        // serverTag国内和国外区分按本地的配置不继承
        setting.beginGroup(CLOUD_GROUP);
        m_fileMap[CLOUD_GROUP][KEY_SERVERTAG] = setting.value(KEY_SERVERTAG).toInt();
        setting.endGroup();

    }
}

template<typename T>
QVariant GlobalConfig::formatValue(T& var, QVariant& value)
{
    if(!value.isValid()) {
        qDebug() << "format Value: " << var;
        return var;
    }
    else
        var = value.value<T>();

    return QVariant();
}

QVariant GlobalConfig::readConfig(int param)
{
    if(static_cast<CommonEnum::ConfigParam>(param) == CommonEnum::LAUNCH_LANGUAGE) {
        return common::readRegister(KEY_LAUNCH_LANGUAGE, m_fileMap[CLOUD_GROUP][KEY_SERVERTAG]).toString().toInt();
    }
    else
    {
        QVariant var = operaConfig(static_cast<CommonEnum::ConfigParam>(param), QVariant());
        return var;
    }
}

QVariant GlobalConfig::writeConfig(int param, QVariant var)
{
    if(static_cast<CommonEnum::ConfigParam>(param) == CommonEnum::LAUNCH_LANGUAGE) {
        common::writeRegister(KEY_LAUNCH_LANGUAGE, var);
        return var;
    }
    else
        return operaConfig(static_cast<CommonEnum::ConfigParam>(param), var);
}

QVariant GlobalConfig::operaConfig(CommonEnum::ConfigParam param, QVariant var)
{
    switch(param) {
        case CommonEnum::CLOUD_USERNAME:
            return formatValue(m_fileMap[CLOUD_GROUP][KEY_USERNAME], var);
        case CommonEnum::CLOUD_USERPWD:
            return formatValue(m_fileMap[CLOUD_GROUP][KEY_USERPWD], var);
        case CommonEnum::CLOUD_ACCOUNTTYPE:
            return formatValue(m_fileMap[CLOUD_GROUP][KEY_ACCOUNT_TYPE], var);
        case CommonEnum::CLOUD_SERVERTAG:
            return formatValue(m_fileMap[CLOUD_GROUP][KEY_SERVERTAG], var);
        case CommonEnum::CLOUD_COUNTRY:
            return formatValue(m_fileMap[CLOUD_GROUP][KEY_CONUTRY], var);
        case CommonEnum::CLOUD_LOGINSTATE:
            return formatValue(m_fileMap[CLOUD_GROUP][KEY_LOGIN_STATE], var);
        case CommonEnum::CLOUD_REMEMBERME:
            return formatValue(m_fileMap[CLOUD_GROUP][KEY_REMEMBER_ME], var);
        case CommonEnum::LAUNCH_LANGUAGE_COUNT:
            return formatValue(m_fileMap[LAUNCH_GROUP][KEY_LAUNCH_COUNT], var);
        case CommonEnum::LAYOUT_PAMCHECKED:
            return formatValue(m_fileMap[LAYOUT_GROUP][KEY_PAM_CHECK], var);
        case CommonEnum::LAYOUT_PSMCHECKED:
            return formatValue(m_fileMap[LAYOUT_GROUP][KEY_PSM_CHECK], var);
        case CommonEnum::LAYOUT_MODELSPACING:
            return formatValue(m_fileMap[LAYOUT_GROUP][KEY_MODEL_SPACING], var);
        case CommonEnum::LAYOUT_PLATFORMMARGINS:
            return formatValue(m_fileMap[LAYOUT_GROUP][KEY_PLATFORM_MARGINS], var);
        case CommonEnum::LAYOUT_RSAZAINDEX:
            return formatValue(m_fileMap[LAYOUT_GROUP][KEY_RSAZA_INDEX], var);
        case CommonEnum::LAYOUT_SBACHECKED:
            return formatValue(m_fileMap[LAYOUT_GROUP][KEY_SBA_CHECK], var);
        case CommonEnum::LAYOUT_SBHCHECKED:
            return formatValue(m_fileMap[LAYOUT_GROUP][KEY_SBH_CHECK], var);
        case CommonEnum::LAYOUT_STAETPOSINDEX:
            return formatValue(m_fileMap[LAYOUT_GROUP][KEY_START_POSITION_INDEX], var);
        case CommonEnum::LAYOUT_MTCANCHECKED:
            return formatValue(m_fileMap[LAYOUT_GROUP][KEY_MTCAN_CHECK], var);

        //qin -- punch
        case CommonEnum::PUNCH_OCD_SPINBOXTEXT:
            return formatValue(m_fileMap[PUNCH_GROUP][KEY_PUNCH_OCD_SPINBOXTEXT], var);
        case CommonEnum::PUNCH_ICD_SPINBOXTEXT:
            return formatValue(m_fileMap[PUNCH_GROUP][KEY_PUNCH_ICD_SPINBOXTEXT], var);
        case CommonEnum::PUNCH_EL_SPINBOXTEXT:
            return formatValue(m_fileMap[PUNCH_GROUP][KEY_PUNCH_EL_SPINBOXTEXT], var);
        case CommonEnum::PUNCH_GD_SPINBOXTEXT:
            return formatValue(m_fileMap[PUNCH_GROUP][KEY_PUNCH_GD_SPINBOXTEXT], var);
        case CommonEnum::PUNCH_DIRECTION_INDEX:
            return formatValue(m_fileMap[PUNCH_GROUP][KEY_PUNCH_DIRECTION_INDEX], var);
        case CommonEnum::PUNCH_KEEPHOLE_CHECKED:
            return formatValue(m_fileMap[PUNCH_GROUP][KEY_PUNCH_KEEPHOLE_CHECK], var);

        //qin -- hollow
        case CommonEnum::HOLLOW_WT_SPINBOXTEXT:
            return formatValue(m_fileMap[HOLLOW_GROUP][KEY_HOLLOW_WT_SPINBOXTEXT], var);
        case CommonEnum::HOLLOW_ACCURACY_SPINBOXTEXT:
            return formatValue(m_fileMap[HOLLOW_GROUP][KEY_HOLLOW_ACCURACY_SPINBOXTEXT], var);
        case CommonEnum::HOLLOW_DIRECTION_INDEX:
            return formatValue(m_fileMap[HOLLOW_GROUP][KEY_HOLLOW_DIRECTION_INDEX], var);
        case CommonEnum::HOLLOW_NBF_INDEX:
            return formatValue(m_fileMap[HOLLOW_GROUP][KEY_HOLLOW_NBF_INDEX], var);
        case CommonEnum::HOLLOW_CELLSIZE_SPINBOXTEXT:
            return formatValue(m_fileMap[HOLLOW_GROUP][KEY_HOLLOW_CELLSIZE_SPINBOXTEXT], var);
        case CommonEnum::HOLLOW_SRADIUS_SPINBOXTEXT:
            return formatValue(m_fileMap[HOLLOW_GROUP][KEY_HOLLOW_SRADIUS_SPINBOXTEXT], var);

        //qin -- recent open file
        case CommonEnum::RECENTOPENFILE_TEXT:
            return formatValue(m_fileMap[RECENTOPENFILE_GROUP][KEY_RECENTOPENFILE_TEXT], var);

        case CommonEnum::REPAIR_MODELQUALITYCHECK:
            return formatValue(m_fileMap[REPAIR_GROUP][KEY_MODEL_QUALITY_CHECK], var);
        case CommonEnum::REPAIR_SMRPDCHECKED:
            return formatValue(m_fileMap[REPAIR_GROUP][KEY_SMRPD_CHECK], var);
        case CommonEnum::SUPPORT_SSMPDWSCHECKBOX:
            return formatValue(m_fileMap[SUPPORT_GROUP][KEY_SUPPORT_CHECKBOX], var);
        case CommonEnum::TEXT_ANGLE:
            return formatValue(m_fileMap[TEXT_GROUP][KEY_TEXT_ANGLE], var);
        case CommonEnum::TEXT_HEIGHT:
            return formatValue(m_fileMap[TEXT_GROUP][KEY_TEXT_HEIGHT], var);
        case CommonEnum::TEXT_TEXT:
            return formatValue(m_fileMap[TEXT_GROUP][KEY_TEXT_TEXT], var);
        case CommonEnum::TEXT_FAMILYINDEX:
            return formatValue(m_fileMap[TEXT_GROUP][KEY_FAMILY_INDEX], var);
        case CommonEnum::TEXT_FONTPOINTINDEX:
            return formatValue(m_fileMap[TEXT_GROUP][KEY_FONT_POINT_INDEX], var);
        case CommonEnum::BASE_CUSTOMSUFFIX:
            return formatValue(m_fileMap[BASE_CONFIG_GROUP][KEY_CUSTOM_SUFFIX], var);
        case CommonEnum::BASE_INTELLIGENTOPEN:
            return formatValue(m_fileMap[BASE_CONFIG_GROUP][KEY_INTELLIGENT_OPEN], var);
        case CommonEnum::BASE_FIRST_FAST_RESIN:
            return formatValue(m_fileMap[BASE_CONFIG_GROUP][KEY_FIRST_FAST_RESIN], var);
        case CommonEnum::BASE_FIRST_HIGH_RESIN:
            return formatValue(m_fileMap[BASE_CONFIG_GROUP][KEY_FIRST_HIGH_RESIN], var);
        case CommonEnum::BASE_TEST_ENVIRONMENT:
            return formatValue(m_fileMap[BASE_CONFIG_GROUP][KEY_TEST_ENVIRONMENT], var);
        case CommonEnum::BASE_UNDOVIEW:
            return formatValue(m_fileMap[BASE_CONFIG_GROUP][KEY_UNDOVIEW], var);
        case CommonEnum::BASE_ENVIRONMENT:
            return formatValue(m_fileMap[BASE_CONFIG_GROUP][KEY_ENVIRONMENT], var);
        case CommonEnum::PATH_FACEGEN:
            return formatValue(m_fileMap[FILE_PATH_CONFIG_GROUP][FACEGEN_FILE_PATH], var);
        case CommonEnum::PATH_PWSP:
            return formatValue(m_fileMap[FILE_PATH_CONFIG_GROUP][PWSP_FILE_PATH], var);
        case CommonEnum::PATH_PWSR:
            return formatValue(m_fileMap[FILE_PATH_CONFIG_GROUP][PWSR_FILE_PATH], var);
        case CommonEnum::PATH_MODEL:
            return formatValue(m_fileMap[FILE_PATH_CONFIG_GROUP][MODEL_FILE_PATH], var);
    default:
        break;
    }

    return QVariant();
}

void GlobalConfig::saveConfig()
{
    mSetting->beginGroup(BASE_CONFIG_GROUP);
    mSetting->setValue(KEY_CUSTOM_SUFFIX, m_fileMap[BASE_CONFIG_GROUP][KEY_CUSTOM_SUFFIX]);
    mSetting->setValue(KEY_INTELLIGENT_OPEN, m_fileMap[BASE_CONFIG_GROUP][KEY_INTELLIGENT_OPEN]);
    mSetting->setValue(KEY_FIRST_FAST_RESIN, m_fileMap[BASE_CONFIG_GROUP][KEY_FIRST_FAST_RESIN]);
    mSetting->setValue(KEY_FIRST_HIGH_RESIN, m_fileMap[BASE_CONFIG_GROUP][KEY_FIRST_HIGH_RESIN]);
    mSetting->setValue(KEY_TEST_ENVIRONMENT, m_fileMap[BASE_CONFIG_GROUP][KEY_TEST_ENVIRONMENT]);
    mSetting->setValue(KEY_ENVIRONMENT, m_fileMap[BASE_CONFIG_GROUP][KEY_ENVIRONMENT]);
    mSetting->setValue(KEY_UNDOVIEW, m_fileMap[BASE_CONFIG_GROUP][KEY_UNDOVIEW]);
    mSetting->endGroup();

    mSetting->beginGroup(FILE_PATH_CONFIG_GROUP);
    mSetting->setValue(FACEGEN_FILE_PATH, m_fileMap[FILE_PATH_CONFIG_GROUP][FACEGEN_FILE_PATH]);
    mSetting->setValue(PWSP_FILE_PATH, m_fileMap[FILE_PATH_CONFIG_GROUP][PWSP_FILE_PATH]);
    mSetting->setValue(PWSR_FILE_PATH, m_fileMap[FILE_PATH_CONFIG_GROUP][PWSR_FILE_PATH]);
    mSetting->setValue(MODEL_FILE_PATH, m_fileMap[FILE_PATH_CONFIG_GROUP][MODEL_FILE_PATH]);
    mSetting->endGroup();

    mSetting->beginGroup(CLOUD_GROUP);
    mSetting->setValue(KEY_USERNAME,        m_fileMap[CLOUD_GROUP][KEY_USERNAME]);
    mSetting->setValue(KEY_USERPWD,         m_fileMap[CLOUD_GROUP][KEY_USERPWD]);
    mSetting->setValue(KEY_ACCOUNT_TYPE,    m_fileMap[CLOUD_GROUP][KEY_ACCOUNT_TYPE]);
    mSetting->setValue(KEY_SERVERTAG,       m_fileMap[CLOUD_GROUP][KEY_SERVERTAG]);
    mSetting->setValue(KEY_CONUTRY,         m_fileMap[CLOUD_GROUP][KEY_CONUTRY]);
    mSetting->setValue(KEY_LOGIN_STATE,     m_fileMap[CLOUD_GROUP][KEY_LOGIN_STATE]);
    mSetting->setValue(KEY_REMEMBER_ME,     m_fileMap[CLOUD_GROUP][KEY_REMEMBER_ME]);
    mSetting->endGroup();

    mSetting->beginGroup(LAUNCH_GROUP);
    mSetting->setValue(KEY_LAUNCH_COUNT, m_fileMap[LAUNCH_GROUP][KEY_LAUNCH_COUNT]);
    mSetting->endGroup();

    mSetting->beginGroup(LAYOUT_GROUP);
    mSetting->setValue(KEY_PAM_CHECK, m_fileMap[LAYOUT_GROUP][KEY_PAM_CHECK]);
    mSetting->setValue(KEY_PSM_CHECK, m_fileMap[LAYOUT_GROUP][KEY_PSM_CHECK]);
    mSetting->setValue(KEY_MODEL_SPACING, m_fileMap[LAYOUT_GROUP][KEY_MODEL_SPACING]);
    mSetting->setValue(KEY_PLATFORM_MARGINS, m_fileMap[LAYOUT_GROUP][KEY_PLATFORM_MARGINS]);
    mSetting->setValue(KEY_RSAZA_INDEX, m_fileMap[LAYOUT_GROUP][KEY_RSAZA_INDEX]);
    mSetting->setValue(KEY_SBA_CHECK, m_fileMap[LAYOUT_GROUP][KEY_SBA_CHECK]);
    mSetting->setValue(KEY_SBH_CHECK, m_fileMap[LAYOUT_GROUP][KEY_SBH_CHECK]);
    mSetting->setValue(KEY_START_POSITION_INDEX, m_fileMap[LAYOUT_GROUP][KEY_START_POSITION_INDEX]);
    mSetting->setValue(KEY_MTCAN_CHECK, m_fileMap[LAYOUT_GROUP][KEY_MTCAN_CHECK]);
    mSetting->endGroup();

    //qin -- punch
    mSetting->beginGroup(PUNCH_GROUP);
    mSetting->setValue(KEY_PUNCH_OCD_SPINBOXTEXT, m_fileMap[PUNCH_GROUP][KEY_PUNCH_OCD_SPINBOXTEXT]);
    mSetting->setValue(KEY_PUNCH_ICD_SPINBOXTEXT, m_fileMap[PUNCH_GROUP][KEY_PUNCH_ICD_SPINBOXTEXT]);
    mSetting->setValue(KEY_PUNCH_EL_SPINBOXTEXT, m_fileMap[PUNCH_GROUP][KEY_PUNCH_EL_SPINBOXTEXT]);
    mSetting->setValue(KEY_PUNCH_GD_SPINBOXTEXT, m_fileMap[PUNCH_GROUP][KEY_PUNCH_GD_SPINBOXTEXT]);
    mSetting->setValue(KEY_PUNCH_DIRECTION_INDEX, m_fileMap[PUNCH_GROUP][KEY_PUNCH_DIRECTION_INDEX]);
    mSetting->setValue(KEY_PUNCH_KEEPHOLE_CHECK, m_fileMap[PUNCH_GROUP][KEY_PUNCH_KEEPHOLE_CHECK]);
    mSetting->endGroup();

    //qin -- hollow
    mSetting->beginGroup(HOLLOW_GROUP);
    mSetting->setValue(KEY_HOLLOW_WT_SPINBOXTEXT, m_fileMap[HOLLOW_GROUP][KEY_HOLLOW_WT_SPINBOXTEXT]);
    mSetting->setValue(KEY_HOLLOW_ACCURACY_SPINBOXTEXT, m_fileMap[HOLLOW_GROUP][KEY_HOLLOW_ACCURACY_SPINBOXTEXT]);
    mSetting->setValue(KEY_HOLLOW_DIRECTION_INDEX, m_fileMap[HOLLOW_GROUP][KEY_HOLLOW_DIRECTION_INDEX]);
    mSetting->setValue(KEY_HOLLOW_NBF_INDEX, m_fileMap[HOLLOW_GROUP][KEY_HOLLOW_NBF_INDEX]);
    mSetting->setValue(KEY_HOLLOW_CELLSIZE_SPINBOXTEXT, m_fileMap[HOLLOW_GROUP][KEY_HOLLOW_CELLSIZE_SPINBOXTEXT]);
    mSetting->setValue(KEY_HOLLOW_SRADIUS_SPINBOXTEXT, m_fileMap[HOLLOW_GROUP][KEY_HOLLOW_SRADIUS_SPINBOXTEXT]);
    mSetting->endGroup();

    //qin -- hollow
    mSetting->beginGroup(RECENTOPENFILE_GROUP);
    mSetting->setValue(KEY_RECENTOPENFILE_TEXT, m_fileMap[RECENTOPENFILE_GROUP][KEY_RECENTOPENFILE_TEXT]);
    mSetting->endGroup();

    mSetting->beginGroup(REPAIR_GROUP);
    mSetting->setValue(KEY_MODEL_QUALITY_CHECK, m_fileMap[REPAIR_GROUP][KEY_MODEL_QUALITY_CHECK]);
    mSetting->setValue(KEY_SMRPD_CHECK, m_fileMap[REPAIR_GROUP][KEY_SMRPD_CHECK]);
    mSetting->endGroup();

    mSetting->beginGroup(SUPPORT_GROUP);
    mSetting->setValue(KEY_SUPPORT_CHECKBOX, m_fileMap[SUPPORT_GROUP][KEY_SUPPORT_CHECKBOX]);
    mSetting->endGroup();

    mSetting->beginGroup(TEXT_GROUP);
    mSetting->setValue(KEY_TEXT_ANGLE, m_fileMap[TEXT_GROUP][KEY_TEXT_ANGLE]);
    mSetting->setValue(KEY_TEXT_HEIGHT, m_fileMap[TEXT_GROUP][KEY_TEXT_HEIGHT]);
    mSetting->setValue(KEY_TEXT_TEXT,m_fileMap[TEXT_GROUP][KEY_TEXT_TEXT]);
    mSetting->setValue(KEY_FAMILY_INDEX, m_fileMap[TEXT_GROUP][KEY_FAMILY_INDEX]);
    mSetting->setValue(KEY_FONT_POINT_INDEX, m_fileMap[TEXT_GROUP][KEY_FONT_POINT_INDEX]);
    mSetting->endGroup();
    mSetting->sync();
}
